<?php
class news
{
	private $newsId;
	private $subject;
	private $detail;
	private $dateadd;
	private $creator;
	private $showstatus;
	private $imgpath;
	
	function setNews($id,$s,$dt,$c,$shs,$img)
	{
		$this->newsId="null";
		$this->subject=addslashes(strip_tags(trim($s)));
		$this->detail=trim($dt);
		$this->dateadd=date("Y-m-d")." ".date("H:i:s");
		$this->creator=trim($c);
		$this->showstatus=trim($shs);
		$this->imgpath=$img;
		if($this->imgpath==""){$this->imgpath="None";}
	}
	private function checkNews()
	{
	if(!empty($this->newsId) && !empty($this->subject) && !empty($this->detail) && !empty($this->creator) && ($this->showstatus!=""))
		return TRUE;		
		else return FALSE;
	}
	function insert()
	{
		if($this->checkNews())
		{

			$sql="INSERT INTO sme_news 
				  VALUES ('$this->newsId',
							'$this->subject',
							'$this->detail',
							'$this->dateadd',
							'$this->creator',
							'$this->showstatus',
							'$this->imgpath')";
			if(mysql_query($sql))
			return TRUE;
			else return FALSE;
		}
		else return FALSE;
	}
	function update($Nid)
	{  
		if($this->checkNews() && !empty($Nid))
		{
			$sql="UPDATE sme_news SET 
					subject = '$this->subject',			
					detail = '$this->detail',
					dateadd = '$this->dateadd',
					creator = '$this->creator',
					showstatus = '$this->showstatus',
					imgpath = '$this->imgpath'
					WHERE newsId ='$Nid'";	
			if(mysql_query($sql))return TRUE;
			else return FALSE;			
		}else return FALSE;
	}
	
	function delete($Nid)
	{
		$sqlpath="SELECT imgpath FROM sme_news WHERE newsId='$Nid'";
		$rs = mysql_query ( $sqlpath );
        $obj = mysql_fetch_array($rs);
		$rowpath = mysql_num_rows($rs);
		if($rowpath>0)
		{
			if($obj[0]!='None')
			$flgDelete = unlink("../new_img/".$obj[0]);
			else $flgDelete = TRUE;
			if($flgDelete)
			{
				$sql="DELETE FROM sme_news WHERE newsID='$Nid'";
				if(mysql_query( $sql ))
				return TRUE;
				else return FALSE;
			}
			else return FALSE;
		}
		else return TRUE;
	}
	function setActiveStatus($id,$ac)
        {
                if(!empty($id))
                  {
                $sql="UPDATE sme_news  SET 
                showstatus = $ac
                WHERE newsId = '$id' ";
                
                if(mysql_query( $sql ))return TRUE;
                else return FALSE;
                }
              else return FALSE;
                }
}


?>